package com.wclass.controller.app;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.collect.Lists;
import com.wclass.common.GeTuiUtil;
import com.wclass.common.ReturnObject;
import com.wclass.entity.BaseAppUser;
import com.wclass.entity.BaseFocus;
import com.wclass.interceptor.BaseContextHandler;
import com.wclass.model.UserMobile;
import com.wclass.service.IBaseAppUserService;
import com.wclass.service.IBaseFocusService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 关注表 前端控制器
 * </p>
 *
 * @author 张浩高
 * @since 2018-12-21
 */
@Api(description = "关注")
@RestController
@RequestMapping("/baseFocus")
public class BaseFocusController {

    @Autowired
    private IBaseFocusService iBaseFocusService;
    @Autowired
    private IBaseAppUserService iBaseAppUserService;

    /**
     * 获取我的粉丝分页
     *
     * @param limit
     * @param offset
     * @return
     */
    @ApiOperation("获取我的粉丝分页")
    @RequestMapping(value = "/getMyFanPage", method = RequestMethod.POST)
    public ReturnObject<Object> getMyFanPage(@ApiParam("每页条数") @RequestParam(defaultValue = "10") int limit,
                                             @ApiParam("页数") @RequestParam(defaultValue = "1") int offset) {
        EntityWrapper<BaseFocus> ew = new EntityWrapper<>();
        ew.eq("focus_user_id", BaseContextHandler.getUserID());
        ew.eq("state", 0);
        ew.orderBy("crt_time", false);
        Page<BaseFocus> obj = iBaseFocusService.selectPage(new Page<>(offset, limit), ew);

        List<UserMobile> list = Lists.newArrayList();
        List<BaseFocus> newFocusList = Lists.newArrayList();
        for (BaseFocus focus : obj.getRecords()) {
            if (focus.getIsNew() == 0) {
                focus.setIsNew(1);
                newFocusList.add(focus);
            }

            UserMobile mobile = new UserMobile();
            BeanUtils.copyProperties(iBaseAppUserService.selectById(focus.getUserId()).setPassword(null), mobile);
            list.add(mobile);
        }
        if (newFocusList.size() > 0)
            iBaseFocusService.updateBatchById(newFocusList);
        
        return new ReturnObject<>(0, "", obj.getTotal(), obj.getPages(), list);
    }

    /**
     * 获取我的关注
     *
     * @param limit
     * @param offset
     * @return
     */
    @ApiOperation("获取我关注的用户分页")
    @RequestMapping(value = "/getMyFocusPage", method = RequestMethod.POST)
    public ReturnObject<Object> getMyFocusPage(@ApiParam("每页条数") @RequestParam(defaultValue = "10") int limit,
                                               @ApiParam("页数") @RequestParam(defaultValue = "1") int offset) {
        EntityWrapper<BaseFocus> ew = new EntityWrapper<>();
        ew.eq("user_id", BaseContextHandler.getUserID());
        ew.eq("state", 0);
        Page<BaseFocus> obj = iBaseFocusService.selectPage(new Page<>(offset, limit), ew);

        List<UserMobile> list = Lists.newArrayList();
        for (BaseFocus focus : obj.getRecords()) {
            UserMobile mobile = new UserMobile();
            BeanUtils.copyProperties(iBaseAppUserService.selectById(focus.getFocusUserId()).setPassword(null), mobile);
            list.add(mobile);
        }

        return new ReturnObject<>(0, "", obj.getTotal(), obj.getPages(), list);
    }

    /**
     * 添加关注
     *
     * @param focusUserId
     * @return
     */
    @ApiOperation("添加关注")
    @RequestMapping(value = "/addFocusUser", method = RequestMethod.POST)
    public ReturnObject addFocusUser(@ApiParam("被关注用户的Id") @RequestParam Long focusUserId) throws Exception {
        if (BaseContextHandler.getUserID() == focusUserId) {
            return new ReturnObject<>(1, "自己不能关注自己");
        }
        EntityWrapper ew = new EntityWrapper();
        ew.eq("user_id", BaseContextHandler.getUserID());
        ew.eq("focus_user_id", focusUserId);
        int count = iBaseFocusService.selectCount(ew);
        if (count == 0) {
            BaseFocus baseFocus = new BaseFocus();
            baseFocus.setUserId(BaseContextHandler.getUserID());
            baseFocus.setIsNew(0);
            baseFocus.setFocusUserId(focusUserId);
            iBaseFocusService.insert(baseFocus);

            BaseAppUser focusUser = iBaseAppUserService.selectOneById(focusUserId);
            String cid = "";
            if (focusUser.getMobileType() != null && focusUser.getMobileType().equals("android")) {
                cid = focusUser.getCId();
            } else if (focusUser.getMobileType() != null && focusUser.getMobileType().equals("ios")) {
                cid = focusUser.getDeviceToken();
            }
            if (cid.length() > 0)
                GeTuiUtil.pushMessageToSingle(focusUser.getMobileType(), cid, BaseContextHandler.getName() + "关注了你");
            return new ReturnObject<>(0, "关注成功");
        }
        return new ReturnObject<>(0, "已经关注,不能重复关注");
    }

    /**
     * 取消关注
     *
     * @param focusUserId
     * @return
     */
    @ApiOperation("取消关注")
    @RequestMapping(value = "/delFocusUser", method = RequestMethod.POST)
    public ReturnObject delFocusUser(@ApiParam("被关注用户的Id") @RequestParam Long focusUserId) {
        EntityWrapper ew = new EntityWrapper();
        ew.eq("user_id", BaseContextHandler.getUserID());
        ew.eq("focus_user_id", focusUserId);
        int count = iBaseFocusService.selectCount(ew);
        if (count != 0) {
            iBaseFocusService.delete(new EntityWrapper<BaseFocus>().eq("user_id", BaseContextHandler.getUserID()).eq
                    ("focus_user_id", focusUserId));

            //用户关注数量修改
            BaseAppUser loginUser = iBaseAppUserService.selectById(BaseContextHandler.getUserID());
            BaseAppUser loginRecord = new BaseAppUser();
            loginRecord.setId(BaseContextHandler.getUserID());
            loginRecord.setFocusNum(loginUser.getFocusNum() - 1);
            iBaseAppUserService.updateById(loginRecord);

            //被关注人粉丝数量修改
            BaseAppUser focusUser = iBaseAppUserService.selectById(focusUserId);
            BaseAppUser focusRecord = new BaseAppUser();
            focusRecord.setId(focusUserId);
            focusRecord.setFanNum(focusUser.getFanNum() - 1);
            iBaseAppUserService.updateById(focusRecord);

            return new ReturnObject<>(0, "取消关注");
        }
        return new ReturnObject<>(0, "你已经取消关注了");
    }

    @ApiOperation("获取用户粉丝分页")
    @RequestMapping(value = "/getFanPageByUserId", method = RequestMethod.POST)
    public ReturnObject<Object> getFanPageByUserId(@ApiParam("每页条数") @RequestParam(defaultValue = "10") int limit,
                                                   @ApiParam("页数") @RequestParam(defaultValue = "1") int offset,
                                                   @ApiParam("用户id") @RequestParam Long userId) {
        EntityWrapper<BaseFocus> ew = new EntityWrapper<>();
        ew.eq("focus_user_id", userId);
        Page<BaseFocus> obj = iBaseFocusService.selectPage(new Page<>(offset, limit), ew);

        List<UserMobile> list = Lists.newArrayList();
        for (BaseFocus focus : obj.getRecords()) {
            UserMobile mobile = new UserMobile();
            BeanUtils.copyProperties(iBaseAppUserService.selectById(focus.getUserId()).setPassword(null), mobile);
            list.add(mobile);
        }

        return new ReturnObject<>(0, "", obj.getTotal(), obj.getPages(), list);
    }

    @ApiOperation("获取用户关注列表分页")
    @RequestMapping(value = "/getFocusPageByUserId", method = RequestMethod.POST)
    public ReturnObject<Object> getFocusPageByUserId(@ApiParam("每页条数") @RequestParam(defaultValue = "10") int limit,
                                                     @ApiParam("页数") @RequestParam(defaultValue = "1") int offset,
                                                     @ApiParam("用户id") @RequestParam Long userId) {
        EntityWrapper<BaseFocus> ew = new EntityWrapper<>();
        ew.eq("user_id", userId);
        Page<BaseFocus> obj = iBaseFocusService.selectPage(new Page<>(offset, limit), ew);

        List<UserMobile> list = Lists.newArrayList();
        for (BaseFocus focus : obj.getRecords()) {
            UserMobile mobile = new UserMobile();
            BeanUtils.copyProperties(iBaseAppUserService.selectById(focus.getFocusUserId()).setPassword(null), mobile);
            list.add(mobile);
        }

        return new ReturnObject<>(0, "", obj.getTotal(), obj.getPages(), list);
    }

}

